package ru.dc.gui.swing.settings;

import com.jidesoft.dialog.AbstractDialogPage;
import ru.dc.Application;
import ru.dc.object.Settings;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

/**
 * <pre>
 * User: Penkov Vladimir
 * Date: 02.11.2010
 * Time: 16:48:59
 * </pre>
 */
public class DownloadPage extends AbstractDialogPage {
    private JTextField destDir;
    private JButton chooseDir;
    private JPanel rootPanel;

    public DownloadPage() {
        setTitle("Загрузка");

        chooseDir.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser ch = new JFileChooser();
                if (destDir.getText()!=null) {
                    ch.setCurrentDirectory(new File(destDir.getText()));
                }
                ch.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                if (ch.showOpenDialog(DownloadPage.this)==JFileChooser.APPROVE_OPTION) {
                    destDir.setText(ch.getSelectedFile().getAbsolutePath());
                }
            }
        });


        setLayout(new BorderLayout());
        add(rootPanel, BorderLayout.CENTER);
    }

    public void lazyInitialize() {
        final Settings settings = Application.getApplication().getSettings();
        destDir.setText(settings.getDownloadDir()!=null ? settings.getDownloadDir() : "");
    }

    public void apply() {
        Application.getApplication().getSettings().setDownloadDir(destDir.getText());

    }
}
